package com.zhuyuan.controller;

import com.zhuyuan.Entity.Notepad;
import com.zhuyuan.common.R;
import com.zhuyuan.repository.NotepadRepository;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Optional;


@RestController
@Slf4j
@RequestMapping("/articleForm")
@Api(tags = "笔记本相关接口")
public class NotepadController {
    @Autowired
    private NotepadRepository notepadRepository;

    @PostMapping("/saveArticle")
    @ApiOperation("内容的新增修改")
    public R<String> saveArticle(@RequestBody Notepad notepad){
        log.info("文本：{}",notepad);
        notepadRepository.save(notepad);
        return R.success("保存/修改,成功");
    }

    @GetMapping("/fill/{id}")
    @ApiOperation("根据id查询内容")
    public R<Optional<Notepad>> fill(@ApiParam("ID") @PathVariable Integer id){
        log.info("文本id：{}",id);
        Optional<Notepad> notepad = notepadRepository.findById(id);
        return R.success(notepad);
    }

    @GetMapping("/fillAll")
    @ApiOperation("查询所有内容，未使用")
    public R<List<Notepad>> fillAll(){
        List<Notepad> list = notepadRepository.findAll();
        return R.success(list);
    }

    @DeleteMapping("/delete/{id}")
    @ApiOperation("根据id删除内容")
    public R<String> delete(@ApiParam("ID") @PathVariable Integer id){
        log.info("删除：{}",id);
        notepadRepository.deleteById(id);
        return R.success("删除成功");
    }
}
